from selenium import webdriver
import time
from selenium.webdriver.support.wait import WebDriverWait #等待模块
from selenium.webdriver.support import expected_conditions as ec #条件
from selenium.webdriver.common.by import By
import pytest

class opera:

    @pytest.fixture()
    def driver(self):
        chromeDriver = webdriver.Chrome()
        #打开网址
        chromeDriver.get("http://localhost:90")
        #窗口最大化
        chromeDriver.maximize_window()
        yield chromeDriver
        chromeDriver.quit()


    def login_admin(self,chromeDriver,username,passwd):
        #创建浏览器对象

        #输入账号密码
        chromeDriver.find_element(By.XPATH,"//*[@id='username']").send_keys(username)
        chromeDriver.find_element(By.XPATH,"//*[@id='password']").send_keys(passwd)
        #点击登录
        chromeDriver.find_element(By.XPATH,"//*[@id='login_btn']").click()
        time.sleep(5)
        #实际结果
        return  chromeDriver.title

    def login_username(self, chromeDriver, username, passwd):
        # 创建浏览器对象

        # 输入账号密码
        chromeDriver.find_element(By.XPATH, "//*[@id='username']").send_keys(username)
        chromeDriver.find_element(By.XPATH, "//*[@id='password']").send_keys(passwd)
        # 点击登录
        chromeDriver.find_element(By.XPATH, "//*[@id='login_btn']").click()
        # 实际结果
        return chromeDriver.find_element(By.XPATH,"/html/body/div/div/div/form/div[1]/span[1]").text

    def login_password(self, chromeDriver, username, passwd):
        # 创建浏览器对象
        # 输入账号密码
        chromeDriver.find_element(By.XPATH, "//*[@id='username']").send_keys(username)
        chromeDriver.find_element(By.XPATH, "//*[@id='password']").send_keys(passwd)
        # 点击登录
        chromeDriver.find_element(By.XPATH, "//*[@id='login_btn']").click()
        # 实际结果
        return chromeDriver.find_element(By.XPATH,"/html/body/div/div/div/form/div[2]/span[1]").text

    def regesiter(self, chromeDriver, username, passwd,repasswd):
        chromeDriver.find_element(By.XPATH, "/html/body/div/div/div/div[2]/a").click()
        chromeDriver.find_element(By.XPATH, "//*[@id='username']").send_keys(username)
        chromeDriver.find_element(By.XPATH, "//*[@id='password']").send_keys(passwd)
        chromeDriver.find_element(By.XPATH, "//*[@id='repassword']").send_keys(repasswd)
        wdw = WebDriverWait(chromeDriver, 5)
        wdw.until(ec.element_to_be_clickable((By.XPATH, "//*[@id='login_btn']"))).click()
        time.sleep(1)
        return chromeDriver.title

    def reg_usrfailed(self,chromeDriver, username, passwd,repasswd):
        chromeDriver.find_element(By.XPATH, "/html/body/div/div/div/div[2]/a").click()
        chromeDriver.find_element(By.XPATH, "//*[@id='username']").send_keys(username)
        chromeDriver.find_element(By.XPATH, "//*[@id='password']").send_keys(passwd)
        chromeDriver.find_element(By.XPATH, "//*[@id='repassword']").send_keys(repasswd)
        return chromeDriver.find_element(By.XPATH,"/html/body/div/div/div/form/div[1]/span[1]").text

    def reg_repwdfailed(self,chromeDriver, username, passwd,repasswd):
        chromeDriver.find_element(By.XPATH, "/html/body/div/div/div/div[2]/a").click()
        chromeDriver.find_element(By.XPATH, "//*[@id='username']").send_keys(username)
        chromeDriver.find_element(By.XPATH, "//*[@id='password']").send_keys(passwd)
        chromeDriver.find_element(By.XPATH, "//*[@id='repassword']").send_keys(repasswd)
        return chromeDriver.find_element(By.XPATH,"/html/body/div/div/div/form/div[3]/span[1]").text

    def reg_repwdempty(self,chromeDriver, username, passwd,repasswd):
        chromeDriver.find_element(By.XPATH, "/html/body/div/div/div/div[2]/a").click()
        chromeDriver.find_element(By.XPATH, "//*[@id='username']").send_keys(username)
        chromeDriver.find_element(By.XPATH, "//*[@id='password']").send_keys(passwd)
        chromeDriver.find_element(By.XPATH, "//*[@id='login_btn']").click()
        return  chromeDriver.find_element(By.XPATH, "/html/body/div/div/div/form/div[3]/span[1]").text

    def reg_pwdempty(self,chromeDriver, username, passwd,repasswd):
        chromeDriver.find_element(By.XPATH, "/html/body/div/div/div/div[2]/a").click()
        chromeDriver.find_element(By.XPATH, "//*[@id='username']").send_keys(username)
        chromeDriver.find_element(By.XPATH, "//*[@id='login_btn']").click()
        return chromeDriver.find_element(By.XPATH, "/html/body/div/div/div/form/div[2]/span[1]").text

    def asseerts(self,execpt,result,chromeDriver):
        #成功输出success
        if execpt == result:
            print("success")
        #失败打印照片并输出eroor
        else:
            chromeDriver.save_screenshot('eroor.png')
            print('eroor')

